package com.airnewslinkosky.ticket.sql;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.provider.BaseColumns;
import android.util.Log;

import com.airnewslinkosky.ticket.model.Passenger;

public class SqlUtils {

	//取登机人列表
	public static ArrayList<Passenger> getPassenger(Context context){
		ArrayList<Passenger> list = new ArrayList<Passenger>();
		Cursor cur = FavoriteInfoDB.selectPsgInfo(context);
		for(int i=0,count = cur.getCount(); i < count; i++){
			if (i >= 10) {
				break;
			}
			cur.moveToPosition(i);
			Passenger passenger = new Passenger();
			passenger.setPassengerName(cur.getString(cur.getColumnIndex("passengerName")));
			passenger.setPassagerType(cur.getString(cur.getColumnIndex("passengerType")));
			passenger.setPassengerIDType(cur.getString(cur.getColumnIndex("idType")));
			passenger.setPassengerIDNo(cur.getString(cur.getColumnIndex("idNo")));
			passenger.setPassagerBirthday(cur.getString(cur.getColumnIndex("birthday")));
			passenger.setPassengerMobile(cur.getString(cur.getColumnIndex("passengerMobile")));
			list.add(passenger);
		}
		cur.close();
		return list;
	}
	
	//保存登机人
	public static void setPassenger(Context context, Passenger passenger){
		Cursor cur = FavoriteInfoDB.selectPsgInfo(context);
		for(int i=0,count = cur.getCount(); i < count; i++ ){
			cur.moveToPosition(i);
			String name = cur.getString(cur.getColumnIndex("passengerName"));
			String idNo = cur.getString(cur.getColumnIndex("idNo"));
			Log.i("setPassenger idNo",  idNo);
			Log.i("setPassenger name",  name);
			if(name.equals(passenger.getPassengerName()) && idNo.equals(passenger.getPassengerIDNo())){
				FavoriteInfoDB.deletePsgInfo(context, cur.getInt(cur.getColumnIndex(BaseColumns._ID)));
			}
		}
		Log.i("setPassenger passenger.passengerName",  passenger.getPassengerName());
		ContentValues values = new ContentValues();
		values.put("passengerName",passenger.getPassengerName());
		values.put("passengerType", passenger.getPassagerType());
		values.put("idType", passenger.getPassengerIDType());
		values.put("idNo", passenger.getPassengerIDNo());
		values.put("birthday",  passenger.getPassagerBirthday());
		values.put("passengerMobile", passenger.getPassengerMobile());
		
		FavoriteInfoDB.insertPsgInfo(context, values);
	}
}
